const list = [
  '/pages/mall/index',
  '/pages/person/index',
  '/pages/tabBar/coupon/index',
  '/pages/o2o/index',
  '/pages/BaihuoMall/index'
]

export const gotoMiniProgram = (url, type = 'navigateTo') => {
  if (url && url[0] !== '/') {
    url = '/' + url
  }
  if (window.my) {
    if (type === 'navigateBack') {
      window.my[type]({ delta: url })
    } else {
      window.my[type]({ url })
    }
  } else if (window.wx) {
    if (type === 'navigateBack') {
      window.wx.miniProgram[type]({ delta: url })
    } else {
      if (list.includes(url)) {
        window.wx.miniProgram['switchTab']({ url })
      } else {
        window.wx.miniProgram[type]({ url })
      }
    }
  }
}

export const jumpApp = (url, params) => {
  const isIOS = localStorage.getItem('isIOS')
  const appshare = localStorage.getItem('appshare')

  let data = {
    linkAddress: url
  }
  if (params) {
    data = {
      ...data,
      ...params
    }
  }
  if (isIOS) {
    window.webkit && window.webkit.messageHandlers.redirectPage.postMessage(data)
  } else {
    window.android && window.android.redirectPage(JSON.stringify(data))
  }
}

/**
  * 此方法为 H5 跳转本地环境 app , 区别了微信 | 安卓 | iOS 
  * 已实现内部填写了未找到本机汇生活 app 时的访问连接下载步骤
  * @param {*} url 跳转到 app 内部指定页面路径
  */
 export const jumpLocaApp = (url) => {
   console.log('跳转', url)
  let u = navigator.userAgent;
  let isWeixin = u.toLowerCase().indexOf('micromessenger') !== -1; // 微信内
  let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; // android终端
  let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); // ios终端
  const schemeUrl = 'ZBApp://main?zbtype=0&linkAddress=' // ZBApp://main为 汇生活 scheme 协议, 问好后面为内部定义的字段,具体请咨询 iOS 或 安卓
  // 微信内
  if(isWeixin) {
    alert('请在浏览器上打开')
  } else {
      //android端
      if (isAndroid) {
          // 安卓app的 scheme 协议
          window.location.href = schemeUrl + url;
          setTimeout(function(){
              let hidden = window.document.hidden || window.document.mozHidden || window.document.msHidden ||window.document.webkitHidden 
              if(typeof hidden =="undefined" || hidden ==false){
                  // 汇生活 应用宝下载地址 
                  window.location.href = 'https://cdn.zbszkj.com/download/apk/release_v1.2.0.apk';
              }
          }, 2000);
      }
      //ios端
      if (isIOS) {
          // ios的scheme协议
          window.location.href = schemeUrl + url;
          setTimeout(function(){
              let hidden = window.document.hidden || window.document.mozHidden || window.document.msHidden ||window.document.webkitHidden 
              if(typeof hidden == 'undefined' || hidden == false){
                  // 汇生活 App store下载地址
                  window.location.href = 'https://apps.apple.com/cn/app/id1536912713?mt=8';
              }
          }, 2000);
      }
  }
}